import {
  AuditMaterialApi,
  batchAuditMaterial,
} from '@/api/audit/content/material'
import { BatchAuditMaterialReq } from '@/api/audit/content/model/materialModel'
import { MaterialItem } from '@/components/ItemList/MaterialList/types'
import { message } from 'ant-design-vue'
import { computed, ref } from 'vue'
import { AuditMode } from '../types'
import { useStore } from '@/store'

const useQuickAudit = (
  searchApi: AsyncFun,
  asyncToPaixin: (accessList: MaterialItem[]) => Promise<void>,
  resParams: { tableData: MaterialItem[] }
) => {
  const store = useStore()
  /** 审核模式 */
  const auditMode = ref<AuditMode>(AuditMode.NORMAL)

  const violationIds = ref<Set<string>>(new Set([]))
  const unViolationList = ref<MaterialItem[]>([])

  const confirmBatchAudit = async () => {
    const violationList = Array.from(violationIds.value)
    unViolationList.value = resParams.tableData.filter(
      (item) => !violationList.includes(item.id)
    )
    const reqData: BatchAuditMaterialReq = {
      successList: unViolationList.value.map((item) => item.id),
      failList: violationList,
    }
    await batchAuditMaterial(reqData)
    message.success('审核成功！')
    await asyncToPaixin(unViolationList.value)
    await searchApi()
  }
  const batchAuditMaterialLoading = computed(() =>
    store.getters['global/isOpLoading'](AuditMaterialApi.BATCH_AUDTI_MATERIAL)
  )

  return {
    violationIds,
    unViolationList,
    confirmBatchAudit,
    auditMode,
    batchAuditMaterialLoading,
  }
}
export default useQuickAudit
